package com.zat.mes.andrmes.dofun.qualitytest

import android.os.Handler
import android.view.LayoutInflater
import android.view.View
import android.widget.RelativeLayout
import androidx.core.content.ContextCompat
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.bigkoo.pickerview.view.TimePickerView
import com.contrarywind.view.WheelView
import com.google.gson.Gson
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.base.BaseActivity
import com.zat.mes.andrmes.data.LoginResultBean
import com.zat.mes.andrmes.dofun.qualitytest.adapter.QualityHistAdapter
import com.zat.mes.andrmes.dofun.qualitytest.vm.QcPlanViewModel
import com.zat.mes.andrmes.event.QcRecordUpdateEventMessage
import com.zat.mes.andrmes.global.CompanionType
import com.zat.mes.andrmes.utils.DateUtil
import com.zat.mes.andrmes.utils.DisplayUtil
import com.zat.mes.andrmes.utils.PreferencesUtil
import kotlinx.android.synthetic.main.activity_quality_plan.*
import kotlinx.android.synthetic.main.layout_title.*
import kotlinx.android.synthetic.main.layout_title_right_date.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.*

/**
 * 质检记录
 */
class QualityHistActivity : BaseActivity() {

    private var qcModel: QcPlanViewModel? = null
    private lateinit var adpater: QualityHistAdapter
    private var empId: Long = 0
    private var timePicker: TimePickerView? = null
    var startDate = DateUtil.formatDate(Date(), DateUtil.FORMAT_3)
    var showDate: String? = null
    override fun init() {
        initView()
        initData()
    }

    /**
     * 初始化数据
     */
    private fun initData() {

        val loginResultBean = Gson().fromJson(
            PreferencesUtil.getString(CompanionType.LOGIN_RESULT),
            LoginResultBean::class.java
        )
        empId = loginResultBean.empid
        showDate = DateUtil.formatDate(Date(), DateUtil.FORMAT_b)
        setDateText()
        initDatePick()

        adpater = QualityHistAdapter(R.layout.item_quality_hist)
        recyclerQualityPlan.layoutManager = LinearLayoutManager(this)
        recyclerQualityPlan.adapter = adpater
        loadModel()

    }

    /**
     * 加载viewmodel
     */
    private fun loadModel() {
        qcModel = ViewModelProvider(this).get(QcPlanViewModel::class.java)
        qcModel?.getQcDetailDataLList()?.observe(this, androidx.lifecycle.Observer {
            closeLoading()
            if (it == null || it.size <= 0) {
                setEmptyVisible()
            } else {
                setEmptyGone()
            }
            adpater.setNewData(it)
        })

        requestData()
    }

    /**
     * 请求数据
     */
    private fun requestData() {
        qcModel?.loadQcRecordByUser(empId, startDate)
        showLoading()
    }

    /**
     * 初始化view
     */
    private fun initView() {
        setContentView(R.layout.activity_quality_plan)
        tVtitle.text = getString(R.string.title_quality_hist)
        rlReturnTitle.visibility = View.VISIBLE
        rlReturnTitle.setOnClickListener { finish() }
        rlRightComm.visibility = View.VISIBLE
        val inflate = LayoutInflater.from(this).inflate(R.layout.layout_title_right_date, null)
        rlRightComm.addView(inflate)
        val layoutParams: RelativeLayout.LayoutParams =
            inflate.layoutParams as RelativeLayout.LayoutParams
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE)
        layoutParams.rightMargin = DisplayUtil.dip2px(this, 30f)
        rlRightComm.setOnClickListener {
            timePicker?.show()
        }
    }

    /**
     * 初始化时间选择器
     */
    private fun initDatePick() {
        timePicker = TimePickerBuilder(this, object : OnTimeSelectListener {
            override fun onTimeSelect(date: Date?, v: View?) {
                if (date != null) {
                    startDate = DateUtil.formatDate(date, DateUtil.FORMAT_3)
                    showDate = DateUtil.formatDate(date, DateUtil.FORMAT_b)
                    setDateText()
                    requestData()

                }
            }

        }).setSubmitColor(ContextCompat.getColor(this, R.color.color_text_nomal))
            .setCancelColor(ContextCompat.getColor(this, R.color.color_text_nomal))
            .setTitleBgColor(ContextCompat.getColor(this, R.color.color_white))
            .setContentTextSize(18)
            .setLabel("", "", "", "", "", "")
            .setDividerType(WheelView.DividerType.WRAP)
            .setDividerColor(ContextCompat.getColor(this, R.color.color_theme))
            .setLineSpacingMultiplier(2.0f)
            .build()
    }

    /**
     * 设置时间
     */
    fun setDateText() {
        tvTimeTitleRight.text = showDate
    }

    /**
     * 刷新数据
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onQcRecordEvent(event: QcRecordUpdateEventMessage?) {
        Handler().postDelayed(Runnable { requestData() }, 500)
    }
}
